<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <th:block th:include="common/head::head"></th:block>
    <title>项目副本管理</title>
    <style>
        .row_btn {
            margin: 5px 10px;
        }
    </style>
</head>
<body>
<div class="row_btn">
    <button onclick="reloadTable();" class="layui-btn layui-btn-sm">刷新表格</button>
</div>
<table class="layui-table" id="table" lay-filter="table"></table>

</body>
<script type="text/html" id="status_templ">
    <div title="请到控制台中管理项目">
        <input type="checkbox" id="" disabled name="status" {{# if(d.status){ }}checked{{# } }}
               lay-skin="switch"
               lay-text="运行中|未运行">
    </div>
</script>
<script type="text/html" id="port_templ">
    <span copyIdPort="{{d.id}}" title="端口：{{ getItemPort(d) }} 进程id:{{ getItemPId(d) }}">{{ getItemPort(d) }}</span>
</script>

<script type="text/html" id="pid_templ">
    <span copyIdPid="{{d.id}}">{{ getItemPId(d) }}</span>
</script>
<script type="text/html" id="bar_projects">
    <a href="javascript:;" class="layui-btn  layui-btn-sm layui-btn-normal" lay-event="manage">控制台</a>
    <a href="javascript:;" class="layui-btn  layui-btn-sm layui-btn-normal" lay-event="ram">监控</a>
    <a href="javascript:;" class="layui-btn  layui-btn-sm layui-btn-warm" lay-event="del">删除</a>
</script>
<script type="text/javascript">
    var copyTableCol = [
        {field: 'id', title: '副本编号'},
        {field: 'status', title: '状态', templet: "#status_templ",},
        {field: 'pid', title: '进程id', templet: "#pid_templ"},
        {field: 'port', title: '端口号', templet: "#port_templ"},
        {field: 'modifyTime', title: '最后修改时间'},
        {field: 'op', title: '操作', align: 'center', toolbar: '#bar_projects', fixed: 'right', width: "30%"}
    ];

    var cachePortInfo;
    var projectId = getQueryString("id");

    function getCacheItemInfo(data, key) {
        if (!data.status) {
            return "";
        }
        if (!cachePortInfo) {
            return "";
        }
        if (!cachePortInfo[data.id]) {
            return "";
        }
        return cachePortInfo[data.id][key] || "";
    }

    function getItemPort(data) {
        return getCacheItemInfo(data, 'port');
    }

    function getItemPId(data) {
        return getCacheItemInfo(data, 'pid');
    }

    function loadSuccess() {

        tableRender({
            id: 'table',
            elem: '#table',
            cols: [copyTableCol],
            data: [],
            url: "./project_copy_list",
            where: {
                id: getQueryString("id")
            },
            done: function (data) {
                var projects = data.data;
                if (projects) {
                    var ids = [];
                    for (var i = 0; i < projects.length; i++) {
                        var project = projects[i];
                        ids.push(project.id);
                    }
                    getProjectMainPort(ids);
                }
                // 选中
            }
        });

        table.on('tool(table)', function (obj) {
            let data = obj.data;
            let event = obj.event;
            if (!data) {
                return;
            }
            if ('manage' === event) {
                manageApplication(data);
            } else if ('ram' === event) {
                if (!data.status) {
                    layer.msg('请先运行程序！');
                    return;
                }
                var url = "./manage/internal?tag=" + projectId + "&copyId=" + data.id;
                tabChange({
                    id: "ram_" + projectId + "_" + data.id,
                    url: url,
                    title: projectId + ' -监控' + data.id,
                });
            } else if ('del' === event) {
                if (data.status) {
                    layer.msg('请先停止程序！');
                    return;
                }
                layer.confirm('确定删除【' + data.id + '】该副本吗？', {
                    title: '系统提示'
                }, function (index) {
                    loadingAjax({
                        url: './deleteProject',
                        data: {
                            id: projectId,
                            copyId: data.id
                        },
                        success: function (data) {
                            layer.msg(data.msg);
                            if (data.code === 200) {
                                reloadTable()
                            }
                        }
                    });
                });
            }
        });

        // 管理
        function manageApplication(data) {
            var url = './manage/console?id=' + projectId + "&copyId=" + data.id;
            tabChange({
                id: projectId + "_" + data.id,
                url: url,
                title: projectId + ' -副本管理-' + data.id
            });
        }
    }

    function getProjectMainPort(ids) {
        if (ids.length <= 0) {
            return
        }
        silentAjax({
            url: './getProjectCopyPort',
            data: {
                id: projectId,
                copyIds: JSON.stringify(ids)
            },
            success: function (data) {
                if (200 === data.code) {
                    cachePortInfo = data.data;
                    for (var key in data.data) {
                        $("span[copyIdPid='" + key + "']").text(data.data[key].pid);
                        $("span[copyIdPort='" + key + "']").text(data.data[key].port);
                    }
                }
            }
        });
    }

    function reloadTable() {
        table.reload('table');
    }
</script>
</html>